package com.cf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cf.domain.pojo.Team;
import com.cf.domain.pojo.TeamMember;
import com.cf.mapper.TeamMapper;
import com.cf.service.ITeamMemberService;
import com.cf.service.ITeamService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements ITeamService {

    private final ITeamMemberService teamMemberService;
    @Override
    public Team getByUserId(Long userId) {
        Team team = lambdaQuery().eq(Team::getUserId, userId).one();
        if(team == null){
            team = new Team();
        }
        return team;
    }

    @Override
    public void insert(Team team) {
        baseMapper.insert(team);
        Long teamId = team.getId();
        for(TeamMember t:team.getTeamMembers()){
            t.setTeamId(teamId);
            t.setUserId(team.getUserId());
            teamMemberService.save(t);
        }
    }

    @Override
    public int updateTeam(Team team) {
        teamMemberService.deleteTeamMemberByTeamId(team.getId());
        for(TeamMember t:team.getTeamMembers()){
            t.setTeamId(team.getId());
            t.setUserId(team.getUserId());
            teamMemberService.save(t);
        }
        return baseMapper.updateTeam(team.getId(), team.getTeamName(), team.getCoach(),team.getPlan(), team.getVideo());
    }
}
